package com.adopt.controller;

import com.adopt.pojo.Admin;
import com.adopt.pojo.Result;
import com.adopt.service.AdminService;
import com.adopt.util.Message;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * ClassName:TestController
 * Package:com.adopt.controller
 * Description:
 *
 * @Date:2023/9/8 16:25
 * Author:shifan
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @RequestMapping("/toAdmin")
    public String toAdmin(Model model){

        List<Admin> admins=adminService.getAdmins();

        model.addAttribute("admins",admins);

        return "admin/admin";
    }

    @RequestMapping("/getAdminByAdminName")
    @ResponseBody
    public Result getAdminByAdminName(String adminName){

        return adminService.getAdminByAdminName(adminName);
    }



    @RequestMapping("/admins")
    @ResponseBody
    public Message getAdmins(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        // 引入PageHelper分页插件
        // 在查询之前只需要调用，传入页码，以及每页的大小
        PageHelper.startPage(pn,4);

        List<Admin> admins = adminService.getAdmins();
        // startPage后面紧跟的这个查询就是一个分页查询
        System.out.println(admins+"admins");

        // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
        // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数

        PageInfo page=new PageInfo(admins,2);

        return Message.success().add("pageInfo",page);

    }


    @RequestMapping("/create")
    @ResponseBody
    public String addAdmin(Admin admin,Model model){
        Result result= adminService.addAdmin(admin);

        if(result.getCode()==200){

            return "redirect:/admin/list";
        }else{

            model.addAttribute("msg",result.getMsg());

            return "redirect:/admin/create";
        }
    }


    @RequestMapping("/delete")
    @ResponseBody
    public String deleteAdmin(Integer id,Model model){
        Result result=adminService.deleteAdmin(id);

        if(result.getCode()==100){

            return toAdmin(model);
        }
        return null;
    }


    @RequestMapping("/update")
    @ResponseBody
    public String updateAdmin(Admin admin, MultipartFile file, Model model){

        if(file!=null && file.equals("")==false) {

            String load = FileLoad.load(file);
            //admin.setPic(load)：将文件路径设置到管理员对象的图片属性中。
            admin.setPic(load);
        }
        Result result= adminService.updateAdmin(admin);

        if(result.getCode()==100){
            return toAdmin(model);

        }else {
            model.addAttribute("msg",result.getMsg());
            return  null;
        }
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Message findById(Integer id) throws ParseException {
        Admin admin = adminService.findById(id);
        if(admin!=null){
            return Message.success().add("admin",admin);
        }else{
            return Message.fail();
        }
    }



    @RequestMapping("/findByName")
    @ResponseBody
    public Message findByName(@RequestParam(value = "pn",defaultValue = "1") Integer pn,String adminName){
        PageHelper.startPage(pn,4);
        //根据管理员名称查询匹配的管理员信息，并返回查询到的管理员对象列表。
        List<Admin> admins = adminService.findByName(adminName);
        if(admins!=null){
            PageInfo page = new PageInfo(admins,2);
            return Message.success().add("pageInfo",page);
        }else{
            return Message.fail();
        }

    }

/*

    @RequestMapping("/test")
    @ResponseBody
    public String test(){

        return "testOK";
    }
*/

}
